/***
 * library 
 */


/******* UI function *******/

/**
 * dropdownlistcheck ui object;
 */
(function($,undefined){
	$.widget("ui.dropdownlistcheck",{
		options:{
			location: "bottom",
			color: "#fff",
			backgroundColor: "#000"
		},
		_create:function(){
			var self = this;
			o = self.options;
			el = self.element;
			var container = $('<div></div>').css({height:"100px",width:"300px",display:"block"});
			container.attr('id','ddlc_'+el.attr('id'));
			container.css("overflow-y","auto");
			//cap = $('<span></span>').css({background:o.backgroundColor,color:o.color});
			el.children().each(function(){
				var check = $('<input />').attr('type','checkbox');
				check.attr('id','chk_'+el.attr('id')+'_'+$(this).val());
				check.val($(this).val());
				var li = $('<div></div>').css({background:o.backgroundColor,color:o.color});
				li.append(check);
				li.append($(this).text());
				container.append(li);
				check.click(function(e){
					e.stopImmediatePropagation();
					var t = $(this).attr('id');
					t= t.split('_');
					var list = [];
					$("#ddlc_"+t[1]+" input:checked").each(function(){
						list[list.length]=$(this).val();
					});
					if(list.length>0) {
						
						var selector = "#"+el.attr('id')+" > option[value="+t[2]+"]";
						$("#"+t[1]).val(list);
						$("#"+t[1]).trigger('change');
					}
				})
			});
			
			//container.append(cap);
			container.insertAfter(el);
			el.css({display:'none'})
			
		},
		destroy:function(){},
		refresh:function(){
			var self = this;
			el = self.element;
			var container = $("#ddlc_"+el.attr('id'));
			container.empty();
			$("#"+el.attr('id')+" option").each(function(){
				var check = $('<input />').attr('type','checkbox');
				check.attr('id','chk_'+el.attr('id')+'_'+$(this).val());
				check.val($(this).val());
				var li = $('<div></div>').css({background:o.backgroundColor,color:o.color});
				li.append(check);
				li.append($(this).text());
				container.append(li);
				check.click(function(e){
					e.stopImmediatePropagation();
					var t = $(this).attr('id');
					t= t.split('_');
					var list = [];
					$("#ddlc_"+t[1]+" input:checked").each(function(){
						list[list.length]=$(this).val();
					});
					if(list.length>0) {		
						var selector = "#"+el.attr('id')+" > option[value="+t[2]+"]";
						$("#"+t[1]).val(list);
					}
				})
			});
			
		}
	});
	
	
	/**$.extend($.ui, { datepicker: { version: "1.0.01" } });
	var PROP_NAME = 'dropdownlistcheck';
	var dpuuid = new Date().getTime();
	var instActive;
	
	function DropDownListCheck(){
		this.debug = false;
	}
	$.extend(DropDownListCheck.prototype,{});
	$.fn.dropdownlistcheck=function(options){
		if ( !this.length ) {
			return this;
		}
		alert('initialized');
	};
	$.dropdownlistcheck = new DropDownListCheck();
	$.dropdownlistcheck.initialized = false;
	$.dropdownlistcheck.version="1.0.01";
	
	window['DDLC_jQuery_' + dpuuid] = $;*/
})(jQuery);
/**
 * end dropdownlistcheck ui object;
 */

(function($,undefined){
	$.widget("ui.ddlstackable",{
		options:{
			location: "bottom",
			color: "#fff",
			backgroundColor: "#000"
		},
		_create:function(){
			var self = this;
			o = self.options;
			var el = self.element;
			var ddl = $("<select></select>");
			var divui = $("<div></div>");
			
			var divcontainer=$("<div></div>");
			divcontainer.attr("id","ddlstackable_stack_"+el.attr('id'));
			ddl.attr("id","ddlstackable_"+el.attr('id'));
			//el.css({display:"none"});
			el.attr("multiple","multiple");
			el.hide();
			//el.attr("multiple","multiple");
			el.children().each(function(){
				var clone = $(this).clone(true);
				ddl.append(clone);
			});
			
			ddl.change(function(e){
				e.stopImmediatePropagation();
				if(el.val()==null || el.val()=="NULL") {
					var vl = $(this).val();
					var newArray = $.merge([],vl);
					el.val(newArray);
				}
				else {
					var valar = el.val();
					var newMergeArray = $.merge(valar,$(this).val());
					el.val(newMergeArray);
				}
				d = $('<div></div>');
				d.addClass('ui-ddlstackable');
				selector = "#"+ddl.attr("id")+" option[value="+$(this).val()+"]";
				$(selector).each(function(){
					//ditem=$('<div></div>');
					//ditem.addClass("ui-ddlstackable-itemcontainer");
					d.append($("<label></label>").append($(this).attr("label")));
					bt = $('<input></input>');
					bt.attr('id','ddlstackable_'+el.attr('id')+'_'+$(this).val());
					bt.addClass("ui-ddlstackable-drop");
					bt.attr("alt","drop");
					//bt.append('&nbsp;')
					val = $(this).val();
					bt.click(function(e){
						e.stopImmediatePropagation();
						var tt = $(this).attr('id').split('_');
						selector = "#"+ddl.attr("id")+" option[value="+tt[2]+"]";
						$(selector).removeAttr("disabled");
						if(jQuery.inArray(tt[2],el.val())>-1) {
							val = el.val();
							val.splice(1,jQuery.inArray(tt[2],el.val()));
							el.val(val);
						}
						$(this).parent().remove();
						
					});
					d.append(bt);
					$(this).attr("disabled","disabled");
				});
				$(this).val(0);
				divcontainer.append(d);
			});
			//ddl.insertBefore(el);
			divui.append(ddl);
			divui.append(divcontainer);
			divui.insertBefore(el);
			$("#"+el.attr('id')+" option:selected").each(function(){
				d = $('<div></div>');
				d.addClass('ui-ddlstackable');
				if($(this).val()!="NULL" && $(this).val()!=null) {
					selector = "#"+ddl.attr("id")+" option[value="+$(this).val()+"]";
					$(selector).each(function(){$(this).attr("disabled","disabled");});
					
					d.append($("<label></label>").append($(this).attr("label")));
					bt = $('<input></input>');
					bt.attr('id','ddlstackable_'+el.attr('id')+'_'+$(this).val());
					bt.addClass("ui-ddlstackable-drop");
					bt.attr("alt","drop");
					//bt.append('&nbsp;')
					val = $(this).val();
					bt.click(function(e){
						e.stopImmediatePropagation();
						var tt = $(this).attr('id').split('_');
						selector = "#"+ddl.attr("id")+" option[value="+tt[2]+"]";
						$(selector).removeAttr("disabled");
						if(jQuery.inArray(tt[2],el.val())>-1) {
							val = el.val();
							val.splice(1,jQuery.inArray(tt[2],el.val()));
							el.val(val);
						}
						$(this).parent().remove();
						
					});
					d.append(bt);
					divcontainer.append(d);
				}
			});
			ddl.val(0);
		},
		destroy:function(){},
		refresh:function(){}
		
	});
})(jQuery);
/**ui plugin zipcode**/
(function($,undefined){
	$.widget("ui.zipplugin",{
		options:{},
		data:[],
		_create:function(){
			var self = this;
			var el = self.element;
			var elid = el.attr('id');
			var divplugin = $("<div></div>");
			
			var container = $('<div></div>');
			container.attr("id","ui-zipplugin-container");
			container.addClass("ui-zipplugin-container");
			searchinput = $('<input type="text" />');
			searchinput.attr("id","ui-zipplugin-"+elid);
			searchinput.val("search...");
			/*searchinput.keyup(function(e){
				e.stopImmediatePropagation();
				if($(this).val().length> 2 && $(this).val() != "search...") {
					alert($(this).val());
				}
			});*/
			searchinput.autocomplete({
				minLength:2,
				source:function(request,response){
					var url = "http://bestcoming.localhost/training/commune/dep/";
					url =url+request.term;
					var rep = response;
					$.ajax({
						type:"GET",
						url : url,
						success:function(data){
							rep(jQuery.parseJSON(data));
						}
					});
					},
				select:function(event,ui){
					selector = "#"+elid+" option[value="+ui.item.value+"]";
					var item = $("#"+elid+" option[value="+ui.item.value+"]");
					if(!item.attr('label')) {
						var li = $('<li></li>').append(ui.item.label);
						li.addClass('ui-autocomplete-item');
						li.attr('id',ui.item.value);
						var bt = $('<a href="#"></input>');
						bt.addClass('ui-autocomplete-item-drop');
						bt.append("&nbsp;");
						var vlue = ui.item.value;
						bt.click(function(event){
							event.stopImmediatePropagation();
							if(jQuery.inArray(vlue,el.val())>-1) {
								vl = el.val();
								vl.splice(1,jQuery.inArray(vlue,vl));
								el.val(vl);
							}
							el.children('option[value='+vlue+']').remove();
							$(this).parent().remove();
						});
						li.append(bt);
						var option = $('<option label="'+ui.item.label+'">'+ui.item.value+'</option>');
						$("#"+elid).append(option);
						var val = $("#"+elid).val();
						selectedvalue = ui.item.value;
						selectedvalue = [selectedvalue];
						if(val) {
							val = $.merge(val,selectedvalue);
						}
						else {val = selectedvalue;}
						$("#"+elid).val(val);
						$('.ui-zipplugin-list').append(li);
					}
					ui.item.value="";
				},
				autoFocus:true});
			
			container.append(searchinput);
			var ul = $("<ul></ul>").addClass("ui-zipplugin-list");
			container.append($("<div></div>").append(ul));
			$("#"+el.attr('id')+" option:selected").each(function(){
				var li = $('<li></li>').append($(this).text());
				li.attr('id',$(this).val());
				li.addClass('ui-autocomplete-item');
				var bt = $('<a href="#"></a>');
				bt.addClass('ui-autocomplete-item-drop');
				bt.append("&nbsp;");
				var vlue = $(this).val();
				bt.click(function(event){
					event.stopImmediatePropagation();
					if(jQuery.inArray(vlue,el.val())>-1) {
						vl = el.val();
						vl.splice(1,jQuery.inArray(vlue,vl));
						el.val(vl);
					}
					el.children('option[value='+vlue+']').remove();
					$(this).parent().remove();
				});
				li.append(bt);
				
				ul.append(li);
			});
			el.hide();
			divplugin.append(container);
			//container.insertAfter(el);
			divplugin.insertBefore(el);
		},
		destroy:function(){},
		_getData:function(){
			var data=[];
			$("ul.ui-zipplugin-list > li.ui-autocomplete-item").each(function(){
				data[data.length]=$(this).attr('id');
			});
			return data;
		}
	});
})(jQuery);


